﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test_ControleBox
{
    public partial class Form1 : Form
    {
        private ContextMenuStrip _menu;

        public Form1()
        {
            InitializeComponent();
            ControleBox.Controle<Button> c = new ControleBox.Controle<Button>();
            c.Label.Tag = c;
            c.Label.MouseClick += Label_MouseClick<Button>;
            this.ShowForm2<Button>(c);
            controleBox1.CreateControl<Button>(c);

            ControleBox.Controle<CheckBox> c2 = new ControleBox.Controle<CheckBox>();
            c2.Label.Tag = c2;
            c2.Label.MouseClick += Label_MouseClick<CheckBox>;
            this.ShowForm2<CheckBox>(c2);
            controleBox1.CreateControl<CheckBox>(c2);

            ControleBox.Controle<Label> c3 = new ControleBox.Controle<Label>();
            c3.Label.Tag = c3;
            c3.Label.MouseClick += Label_MouseClick<Label>;
            this.ShowForm2<Label>(c3);
            controleBox1.CreateControl<Label>(c3);

            ControleBox.Controle<ComboBox> c4 = new ControleBox.Controle<ComboBox>();
            c4.Label.Tag = c4;
            c4.Label.MouseClick += Label_MouseClick<ComboBox>;
            this.ShowForm2<ComboBox>(c4);
            controleBox1.CreateControl<ComboBox>(c4);
        }

        private void button_Click<T>(object sender, EventArgs e)
            where T : Control, new()
        {
            ControleBox.Controle<T> c = new ControleBox.Controle<T>();
            c.Label.Tag = c;
            c.Label.MouseClick += Label_MouseClick<T>;
            this.ShowForm2<T>(c);
            controleBox1.CreateControl<T>(c);
        }

        void Label_MouseClick<T>(object sender, MouseEventArgs e)
            where T : Control, new()
        {
            if (e.Button == MouseButtons.Right)
            {
                _menu = new ContextMenuStrip();
                ToolStripItem[] t = {new ToolStripButton("Modifier"),
                                     new ToolStripButton("Supprimer"),
                                     new ToolStripButton("Monter")};
                foreach (ToolStripItem item in t)
                {
                    item.Tag = ((Control)sender).Tag;
                }
                t[0].Click += Modif_Click<T>;
                t[1].Click += Suppr_Click<T>;
                t[2].Click += Up_Click<T>;

                _menu.ShowImageMargin = false;
                _menu.Items.AddRange(t);
                _menu.Show((Label)sender, e.Location);
            }
        }

        void Up_Click<T>(object sender, EventArgs e)
            where T : Control, new()
        {
            ToolStripItem cb = (ToolStripItem)sender;
            controleBox1.MoveUp<T>((ControleBox.Controle<T>)cb.Tag);
        }

        private void Modif_Click<T>(object c, EventArgs e)
            where T : Control, new()
        {
            ToolStripItem cb = (ToolStripItem)c;
            this.ShowForm2<T>((ControleBox.Controle<T>)cb.Tag);
        }

        private void ShowForm2<T>(ControleBox.Controle<T> c)
            where T : Control, new()
        {
            Form2<ControleBox.Controle<T>> f = new Form2<ControleBox.Controle<T>>(c);
            f.ShowDialog(this);
        }

        private void Suppr_Click<T>(object c, EventArgs e)
            where T : Control, new()
        {
            ToolStripItem cb = (ToolStripItem)c;
            this.controleBox1.removeControl<T>((ControleBox.Controle<T>)cb.Tag);
        }
    }
}
